$(document).ready   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
dl 0
loc 5
rs 10
c 1
b 0
f 0
cc 2
nc 2
nop 1
1
/**
2
 * telegram.send main scripts
3
 */
4
5
var user
6
7
jQuery(document).ready(function () {
8
9
    // Выбор select без ctrl
10
    $('select').mousedown(function (event) {
11
        event.preventDefault()
12
13
        var select = this
14
        var scroll = select.scrollTop
15
        event.target.selected = !event.target.selected
16
        setTimeout(function () {select.scrollTop = scroll}, 0)
17
        $('select').focus()
18
    }).mousemove(function (e) {e.preventDefault()})
19
20
    // false для checkbox
21
    $('.module_on:checkbox').on('change', function () {
22
        if (this.checked) {
23
            $(this).val('1')
24
        } else {
25
            $(this).val('0')
26
        }
27
    })
28
29
    $('.proxy_on:checkbox').on('change', function () {
30
        if (this.checked) {
31
            $(this).val('1')
32
        } else {
33
            $(this).val('0')
34
        }
35
    })
36
37
    // Сохранение настроек
38
    $('#save').click(function (event) {
39
        event.preventDefault()
40
        var btn = this
41
42
        $(btn).fadeTo(0, 0.5)
43
        $.ajax({
44
            url: '/bitrix/admin/telegram_main.php',
45
            type: 'POST',
46
            dataType: 'json',
47
            data: {
48
                funcName: 'saveConfig',
49
                fields: {
50
                    module_on: $('.module_on').val(),
51
                    name_bot: $('.name_bot').val(),
52
                    token: $('.token').val(),
53
                    mail: $('.mail').val(),
54
                    proxy_on: $('.proxy_on').val(),
55
                    proxy: {
56
                        url: $('.proxy_url').val(),
57
                        port: $('.proxy_port').val(),
58
                        user: $('.proxy_user').val(),
59
                        pass: $('.proxy_pass').val()
60
                    }
61
                }
62
            }
63
        }).done(function (data) {
64
            if (Object.keys(data['message']).length > 0) {
65
                $('.telegram-response').html(data['message']).fadeIn(500)
66
            }
67
        }).fail(function (data) {
68
            console.log(data)
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
69
            alert('Error. Please, refresh page!')
0 ignored issues
show
Debugging Code Best Practice introduced by
The alert UI element is often considered obtrusive and is generally only used as a temporary measure. Consider replacing it with another UI element.
Loading history...
70
        }).always(function () {
71
            $(btn).fadeTo(0, 1)
72
        })
73
    })
74
75
    // Получение входящих запросов
76
    $('#updates_user').click(function (event) {
77
        event.preventDefault()
78
        var btn = this
79
80
        $(btn).fadeTo(0, 0.5)
81
        $.ajax({
82
            type: 'POST',
83
            dataType: 'json',
84
            url: '/bitrix/admin/telegram_main.php',
85
            data: {
86
                funcName: 'getUpdates'
87
            }
88
        }).done(function (data) {
89
            user = data['updates']
90
91
            if (Object.keys(user).length > 0) {
92
                $('.new_user').html('<td colspan="6">Новый пользователь</td>')
93
94
                $('.telegram_user').html(
95
                    '<td id="idUser">' + user['id'] + '</td>' +
96
                    '<td id="nicknameUser">' + user['username'] + '</td>' +
97
                    '<td id="usernameUser">' + user['first_name'] + ' ' + user['last_name'] + '</td>' +
98
                    '<td><input id="save_user" name="save_user" value="Сохранить" type="button" onclick="saveUser();"></td>'
99
                )
100
            } else {
101
                $('.telegram-response').html(data['message']).fadeIn(500)
102
            }
103
104
        }).fail(function (data) {
105
            console.log(data)
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
106
            alert('Error. Please, refresh page!')
0 ignored issues
show
Debugging Code Best Practice introduced by
The alert UI element is often considered obtrusive and is generally only used as a temporary measure. Consider replacing it with another UI element.
Loading history...
107
        }).always(function () {
108
            $(btn).fadeTo(0, 1)
109
        })
110
    })
111
112
    // Переключение табов
113
    $('.adm-detail-tabs-block span').click(function() {
114
        var click_id=$(this).attr('id');
115
        if (click_id !== $('.adm-detail-tabs-block span.adm-detail-tab-active').attr('id') ) {
116
            $('.adm-detail-tabs-block span').removeClass('adm-detail-tab-active');
117
            $(this).addClass('adm-detail-tab-active');
118
            $('.adm-detail-content').fadeOut(0);
119
            $('#wrap-' + click_id).fadeIn(500);
120
        }
121
    })
122
123
})
124
125
function saveUser () {
126
    $.ajax({
127
        type: 'post',
128
        dataType: 'json',
129
        url: '/bitrix/admin/telegram_main.php',
130
        data: {
131
            funcName: 'setUser',
132
            fields: {
133
                id: user['id'],
134
                nickname: user['username'],
135
                username: user['first_name'] + ' ' + user['last_name']
136
            }
137
        }
138
    }).done(function (data) {
139
        $('.new_user').hide()
140
        $('#save_user').hide()
141
142
        if (Object.keys(data['message']).length > 0) {
143
            $('.telegram-response').html(data['message']).fadeIn(500)
144
        }
145
146
    }).fail(function (data) {
147
        console.log(data)
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
148
        alert('Error. Please, refresh page!')
0 ignored issues
show
Debugging Code Best Practice introduced by
The alert UI element is often considered obtrusive and is generally only used as a temporary measure. Consider replacing it with another UI element.
Loading history...
149
    })
150
}
151
152
function delUser (user_id) {
153
    $.ajax({
154
        type: 'post',
155
        dataType: 'json',
156
        url: '/bitrix/admin/telegram_main.php',
157
        data: {
158
            funcName: 'deleteUser',
159
            fields: {
160
                id: user_id
161
            }
162
        }
163
    }).done(function (data) {
164
        $('#' + user_id).hide()
165
166
        if (Object.keys(data['message']).length > 0) {
167
            $('.telegram-response').html(data['message']).fadeIn(500)
168
        }
169
170
    }).fail(function (data) {
171
        console.log(data)
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
172
        alert('Error. Please, refresh page!')
0 ignored issues
show
Debugging Code Best Practice introduced by
The alert UI element is often considered obtrusive and is generally only used as a temporary measure. Consider replacing it with another UI element.
Loading history...
173
    })
174
}
175